<template>
  <div>
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="../assets/heima.png" />
          <span>电商后台管理项目</span>
        </div>
        <el-button type="info" @click="logOut">退出</el-button>
      </el-header>
      <!-- 主体区域 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse?'64px':'200px'">
          <div class="toggle-button" @click="taggleCollapse()">|||</div>
          <!-- 侧边栏菜单 -->
          <el-menu
            background-color="#545c64"
            text-color="#fff"
            active-text-color="skyblue"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="defaultActive"
          >
            <!-- 一级菜单 -->
            <el-submenu :index="item.id +''" v-for="item in menulist" :key="item.id">
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="menuiconlist[item.id]"></i>
                <!-- 文字 -->
                <span>{{item.authName}}</span>
              </template>

              <!-- 二级菜单区域 -->

              <el-menu-item
                :index="'/'+subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="active('/'+subItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!--内容区域  -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单栏数据
      menulist: [],
      menuiconlist: {
        "125": "el-icon-s-custom",
        "103": "el-icon-s-tools",
        "101": "el-icon-goods",
        "102": "el-icon-s-order",
        "145": "el-icon-s-data",
      },
      isCollapse: false,
      defaultActive: "",
    };
  },
  created() {
    this.getMenuList();
    this.defaultActive = window.sessionStorage.getItem("defaultactive");
  },
  methods: {
    logOut() {
      // 清空token
      window.sessionStorage.clear();
      // 跳转到登录页
      this.$router.push("/login");
    },
    // 获取左侧菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      console.log(res);
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      }
      this.menulist = res.data;
    },
    taggleCollapse() {
      console.log("ok");
      this.isCollapse = !this.isCollapse;
    },
    active(str) {
      window.sessionStorage.setItem("defaultactive", str);
    },
  },
};
</script>
<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  color: #fff;
  font-size: 24px;
  display: flex;
  justify-content: space-between;
  padding: 10px 15px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-menu {
  border-right: none;
}
.toggle-button {
  background-color: grey;
  color: #fff;
  font-size: 10px;
  letter-spacing: 0.2em;
  height: 24px;
  line-height: 24px;
  text-align: center;
}
.el-aside {
  cursor: pointer;
}
</style>